package com.by.redis.utility.common;

import com.by.redis.consts.ByRedisConst;
import com.by.redis.support.RedisTemplateSupport;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;


@Slf4j
@Configuration
@ConditionalOnProperty(name = "by.redis.common.enabled", havingValue = "true")
public class ByCommonRedisConfig extends CachingConfigurerSupport {

    @Resource
    private ByCommonRedisLettuceBuilder byCommonRedisLettuceBuilder;

    @Bean(ByRedisConst.COMMON_STR_REDIS_TEMPLATE_NAME)
    public StringRedisTemplate stringRedisTemplate() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(byCommonRedisLettuceBuilder.getFactory());
        return template;
    }

    @Bean(ByRedisConst.COMMON_JSON_REDIS_TEMPLATE_NAME)
    public RedisTemplate<String, Object> jsonRedisTemplate() {
        return RedisTemplateSupport.buildJsonRedisTemplate(byCommonRedisLettuceBuilder.getFactory());
    }

}
